#include"sort.h"
#include"Stack.h"
void Swap(int* a, int* b) {
	int tmp = *a;
	*a = *b;
	*b = tmp;
}


void Print(int* arr, int sz) {
	for (int i = 0; i < sz; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void BubbleSort(int* arr, int sz) {
	for (int i = 0; i < sz - 1; i++) {
		int flag = 0;
		for (int j = 0; j < sz - 1 - i; j++) {
			if (arr[j] > arr[j + 1]) {
				Swap(&arr[j], &arr[j + 1]);
				flag = -1;
			}
		}
		if (flag == 0) break;
	}
}

void InsertSort(int* arr, int sz) {
	for (int i = 0; i < sz - 1; i++) {
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0) {
			if (arr[end] > tmp) {
				arr[end + 1] = arr[end];
				end--;
			}
			else break;
		}
		arr[end + 1] = tmp;
	}
}
void ShellSort(int* arr, int sz) {
	int gap = sz;
	while (gap > 1) {
		gap = gap / 3 + 1;
		for (int i = 0; i < sz - gap; i++) {
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0) {
				if (arr[end] > tmp) {
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else break;
			}
			arr[end + gap] = tmp;
		}

	}
}
void SelectSort(int* arr, int sz) {
	int begin = 0, end = sz - 1;
	while (begin < end) {
		int mini = begin;
		int maxi = end;
		for (int i = begin; i <= end; i++) {
			if (arr[mini] > arr[i]) {
				mini = i;
			}
			if (arr[maxi] < arr[i]) {
				maxi = i;
			}
		}
		if (maxi = begin) {
			maxi = mini;
		}
		Swap(&arr[mini], &arr[begin++]);
		Swap(&arr[maxi], &arr[end--]);
	}
}
int _QuickSort(int* arr, int left, int right) {
	int keyi = left;
	left++;
	while (left <= right) {
		while (left <= right && arr[left] < arr[keyi]) {
			left++;
		}
		while (left <= right && arr[right] > arr[keyi]) {
			right--;
		}
		if (left <= right) {
			Swap(&arr[left++], &arr[right--]);
		}
	}
	Swap(&arr[keyi], &arr[right]);
	return right;
}//霍尔版本找基准值
int _QuickSort1(int* arr, int left, int right) {
	int hole = left;
	int key = arr[left];
	while (left < right) {
		while (left <right && arr[right] >= key) {
			right--;
		}
		arr[hole] = arr[right];
		hole = right;
		while (left < right && arr[right] <= key) {
			left++;
		}
		arr[hole] = arr[left];
		hole = left;
	}
	arr[hole] = key;
	return hole;
}//挖坑法
int _QuickSort2(int* arr, int left, int right) {
	int cur = left + 1;
	int prev = left;
	int keyi = left;
	while (cur <= right) {
		if (arr[cur] < arr[keyi] && ++prev != cur) {
			Swap(&arr[cur], &arr[prev]);
		}
		cur++;
	}
	Swap(&arr[prev], &arr[keyi]);

	return prev;
}
void QuickSort(int* arr, int left ,int right) {
	if (left >= right) return;
	int keyi = _QuickSort2(arr, left, right);
	QuickSort(arr, left, keyi - 1);
	QuickSort(arr, keyi + 1, right);
}
void QuickSortNonR(int* arr, int left, int right) {
	Stack st;
	StInit(&st);
	StPush(&st,left);
	StPush(&st,right);
	while (!StEmpty(&st)) {
		int end = StTop(&st);
		StPop(&st);
		int begin = StTop(&st);
		StPop(&st);

		int prev = begin;
		int cur = begin + 1;
		int keyi = begin;
		while (cur <= end) {
			if (arr[cur] < arr[keyi] && ++prev != cur) {
				Swap(&arr[cur], &arr[prev]);
			}
			cur++;
		}
		Swap(&arr[prev], &arr[keyi]);
		keyi = prev;

		if (begin < keyi - 1) {
			StPush(&st, begin);
			StPush(&st, keyi - 1);
		}
		if (end > keyi + 1) {
			StPush(&st, keyi + 1);
			StPush(&st, end);
		}

	}
	StDestroy(&st);
}
void _MergeSort(int* arr, int left, int right, int* tmp) {

	if (left >= right) return;
	int mid = (left + right) / 2;

	_MergeSort(arr, left, mid, tmp);
	_MergeSort(arr, mid + 1, right, tmp);

	int begin1 = left;
	int end1 = mid;
	int begin2 = mid + 1;
	int end2 = right;
	int index = left;
	while (begin1 <= end1 && begin2 <= end2) {
		if (arr[begin1] < arr[begin2]) {
			tmp[index++] = arr[begin1++];
		}
		else {
			tmp[index++] = arr[begin2++];
		}
	}
	while (begin1 <= end1) {
		tmp[index++] = arr[begin1++];
	}
	while (begin2 <= end2) {
		tmp[index++] = arr[begin2++];
	}
	for (int i = left; i <= right; i++) {
		arr[i] = tmp[i];
	}
}
void MergeSort(int* arr, int sz) {
	int* tmp = (int*)malloc(sizeof(int) * sz);
	if (tmp == NULL) {
		perror("malloc fail");
		exit(1);
	}
	_MergeSort(arr, 0, sz - 1, tmp);
	free(tmp);
}
void CountSort(int* arr, int sz) {
	int mini = 0, maxi = 0;
	for (int i = 0; i < sz; i++) {
		if (arr[i] < arr[mini]) {
			mini = i;
		}
		if (arr[i] > arr[maxi]) {
			maxi = i;
		}
	}
	int ArrSz = arr[maxi] - arr[mini] + 1;
	int* tmp = (int*)calloc(ArrSz,sizeof(int));
	if (tmp == NULL) {
		perror("malloc fail");
		exit(1);
	}
	
	for (int i = 0; i < sz; i++) {
		tmp[arr[i] - arr[mini]]++;
	}
	int index = 0;
	for (int i = 0; i < ArrSz; i++) {
		while (tmp[i]--) {
			arr[index++] = i + arr[mini];
		}
	}
}
void QuickSortThree(int* arr, int left,int right) {
	if (left >= right) return;
	int begin = left;
	int end = right;
	int cur = left + 1;
	int key = arr[left];
	while (cur <= right) {
		if (arr[cur] < key) {
			Swap(&arr[cur], &arr[left]);
			cur++;
			left++;
		}
		else if (arr[cur] > key) {
			Swap(&arr[cur], &arr[right]);
			right--;
		}
		else cur++;
	}
	QuickSortThree(arr, begin, left - 1);
	QuickSortThree(arr, right + 1, end);
}